package com.fwpsl.spring.Service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class RibbonService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hystrixFallback")
    public String helloService(String serviceId){
        //调用服务提供者接口，正常则返回hello字符串
        return restTemplate.getForEntity(String.format("http://%s/welcome", serviceId), String.class).getBody();
    }

    /**
     * 调用服务失败处理方法
     * @return “error"
     */
    public String hystrixFallback(String serviceId){
        return String.format("服务%s， 请求失败！", serviceId);
    }
}
